package crawler

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

// NetEaseCrawler 新闻抓取程序
type NetEaseCrawler struct {
	ResponseData interface{}
}

// NetEaseChannel 网易新闻频道类型
type NetEaseChannel string

const (

	/*
		https://www.cnblogs.com/zhaoguangzheng/p/10876556.html
		https://3g.163.com
		新闻：/touch/reconstruct/article/list/BBM54PGAwangning/0-10.html
		娱乐：/touch/reconstruct/article/list/BA10TA81wangning/0-10.html
		体育：/touch/reconstruct/article/list/BA8E6OEOwangning/0-10.html
		财经：/touch/reconstruct/article/list/BA8EE5GMwangning/0-10.html
		军事：/touch/reconstruct/article/list/BAI67OGGwangning/0-10.html
		科技：/touch/reconstruct/article/list/BA8D4A3Rwangning/0-10.html
		手机：/touch/reconstruct/article/list/BAI6I0O5wangning/0-10.html
		数码：/touch/reconstruct/article/list/BAI6JOD9wangning/0-10.html
		时尚：/touch/reconstruct/article/list/BA8F6ICNwangning/0-10.html
		游戏：/touch/reconstruct/article/list/BAI6RHDKwangning/0-10.html
		教育：/touch/reconstruct/article/list/BA8FF5PRwangning/0-10.html
		健康：/touch/reconstruct/article/list/BDC4QSV3wangning/0-10.html
		旅游：/touch/reconstruct/article/list/BEO4GINLwangning/0-10.html
		视频：/touch/nc/api/video/recommend/Video_Recom/0-10.do?callback=videoList
	*/

	// NetEaseChannelNews 新闻
	NetEaseChannelNews NetEaseChannel = "BBM54PGAwangning"
	// NetEaseChannelEntertainment 娱乐
	NetEaseChannelEntertainment NetEaseChannel = "BA10TA81wangning"
	// NetEaseChannelSports 体育
	NetEaseChannelSports NetEaseChannel = "BA8E6OEOwangning"
	// NetEaseChannelFinance 财经
	NetEaseChannelFinance NetEaseChannel = "BA8EE5GMwangning"
	// NetEaseChannelMilitary 军事
	NetEaseChannelMilitary NetEaseChannel = "BAI67OGGwangning"
	// NetEaseChannelTech 科技
	NetEaseChannelTech NetEaseChannel = "BA8D4A3Rwangning"
	// NetEaseChannelPhone 手机
	NetEaseChannelPhone NetEaseChannel = "BAI6I0O5wangning"
	// NetEaseChannelDigital 数码
	NetEaseChannelDigital NetEaseChannel = "BAI6JOD9wangning"
	// NetEaseChannelFation 时尚
	NetEaseChannelFation NetEaseChannel = "BA8F6ICNwangning"
	// NetEaseChannelGame 游戏
	NetEaseChannelGame NetEaseChannel = "BAI6RHDKwangning"
	// NetEaseChannelEducation 教育
	NetEaseChannelEducation NetEaseChannel = "BDC4QSV3wangning"
	// NetEaseChannelHealth 健康
	NetEaseChannelHealth NetEaseChannel = "BDC4QSV3wangning"
)

// GetCrawlerURL 返回爬虫目标地址
func (c *NetEaseCrawler) GetCrawlerURL(channelName NetEaseChannel) string {
	const site string = "https://3g.163.com/touch/reconstruct/article/list/%s/0-10.html"
	url := fmt.Sprintf(site, channelName)
	return url
}

// StartCrawler 开始抓取新闻
func (c *NetEaseCrawler) StartCrawler(channelName NetEaseChannel) ([]map[string]interface{}, error) {

	url := c.GetCrawlerURL(channelName)
	res, err := http.Get(url)

	if err != nil {
		log.Println(err)
		return nil, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		log.Printf("status code error: %d %s", res.StatusCode, res.Status)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	// 处理爬取的结果，格式处理
	content := string(body)
	t1 := fmt.Sprintf(`artiList({"%s":`, channelName)
	content = strings.Replace(content, t1, "", -1)
	content = strings.Replace(content, "})", "", -1)

	// log.Println(content)

	list := []map[string]interface{}{}
	json.Unmarshal([]byte(content), &list)

	for _, item := range list {
		item["url"] = fmt.Sprintf("https://3g.163.com/dy/article/%s.html", item["docid"])
	}

	return list, nil
}
